﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using System.Threading;

namespace thinger.VisionDemo
{
    public class FtpUploader
    {
        public void UploadFile(string localFilePath, string remoteFilePath, string ftpServer, string username, string password)
        {

            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServer + "/" + remoteFilePath);
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(username, password);

                // Convert the file content to a byte array.
                byte[] fileContents = File.ReadAllBytes(localFilePath);

                request.ContentLength = fileContents.Length;

                // Buffer for reading data
                byte[] buffer = new byte[2048];

                // Stream to which the file to be upload is written
                Stream requestStream = request.GetRequestStream();

                // Write Buffer to the File Stream
                requestStream.Write(fileContents, 0, fileContents.Length);

                // Close the file stream
                requestStream.Close();

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                Console.WriteLine("Upload Complete, status {0}", response.StatusDescription);

                // Close the response.
                response.Close();
            }
            catch (Exception ex)
            {

                Console.WriteLine("上传错误信息：" + ex.ToString());
            }
        }
    }


    public class FtpDownloader
    {
        public void DownloadFile(string remoteFilePath, string localFilePath, string ftpServer, string username, string password)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServer + "/" + remoteFilePath);
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            request.Credentials = new NetworkCredential(username, password);

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();
            FileStream localFileStream = new FileStream(localFilePath, FileMode.Create);

            byte[] buffer = new byte[2048];
            int bytesRead = responseStream.Read(buffer, 0, buffer.Length);

            // Write the downloaded data to a local file.
            while (bytesRead > 0)
            {
                localFileStream.Write(buffer, 0, bytesRead);
                bytesRead = responseStream.Read(buffer, 0, buffer.Length);
            }

            // Close the response and stream objects
            response.Close();
            localFileStream.Close();
        }
    }

    //// 使用示例
    //class Program
    //{
    //    static void Main(string[] args)
    //    {
    //        FtpDownloader ftpDownloader = new FtpDownloader();
    //        string remoteFilePath = "remote/path/file.txt";
    //        string localFilePath = @"C:\path\to\local\file.txt";
    //        string ftpServer = "ftp://yourftpserver.com";
    //        string username = "yourusername";
    //        string password = "yourpassword";

    //        ftpDownloader.DownloadFile(remoteFilePath, localFilePath, ftpServer, username, password);
    //    }
    //}






}
